<?php

/**
 *
 */
class DB {

    private $db;
    

    function __construct() {
        $this->db = mysqli_connect(_DB_SERVER_, _DB_USER_, _DB_PASSWD_, _DB_NAME_) or die("Opps some thing went wrong with user data");
    }

    public function login($user, $password, $from = 'awesomepeople') {

        $username = addslashes($user);
        $password = addslashes($password);

        $sql = "SELECT id FROM user WHERE username='$username' and password='$password'";
        $result = mysqli_query($this->db, $sql);
        $row = mysqli_fetch_array($result);

        $count = mysqli_num_rows($result);

        if ($count == 1) {
            
            session_start();
            $user_id = $row['id'];
            $_SESSION['session_setup'] = array();
            $_SESSION['awesome_id'] = $user_id;
			header('Location: index.php');
			
            header('Location: index.php');

            return $user_id;
        } else {
            if ($this->signUp($user, $password, $from)) {
                $this->login($user, $password, $from);
            } else {
                return FALSE;
            }
        }
    }

    public function signUp($user, $password, $from = 'awesomepeople') {

        $query = "INSERT INTO user (username, password, oauth_provider) VALUES ('$user','$password','$from')";
        $result = mysqli_query($this->db, $query);
        if (!empty($result))
            return TRUE;
        else
            return FALSE;
    }

    public function getUserArchievements($userID) {
        $sql = "SELECT a.* FROM user_achievement AS ua 
				JOIN achievement AS a ON ua.id_archievement = a.id WHERE ua.id_user = $userID";
        $result = mysqli_query($this->db, $sql);
        $count = mysqli_num_rows($result);
        if($count > 1){


        if ($count > 1) {
            $achievements = array();
            $achievementsUndone = array();

            $response = '{ "achievements" : [';

            while ($row = mysqli_fetch_assoc($result)) {

                $archievement = new Archievement($row['id'], $row['name'], $row['description'], $row['points'], $row['creator'], $row['category'], $row['done'], $row['date_add'], $row['date_finish'], $row['date_limit'], $row['id_visual']);

                if ($row['done'])
                    array_push($achievements, $archievement);
                else
                    array_push($achievementsUndone, $archievement);

                $response .= json_encode($row) . ",";
            }
            $response = substr($response, 0, -1);
            $response .= '] }';


            return array("done" => $achievements, "undone" => $achievementsUndone);
            echo $response;
        }else {
            return array();
        }
    }
    }

    public function createArchievement($archievement){
       //$firebug->log($archievement->getName());
        $sql = "INSERT INTO achievement (name, description, points, creator, category, done, date_add, date_finish, date_limit, id_visual) 
                                    VALUES ('" . $archievement->getName() . "','" . $archievement->getDescription() . "'," . $archievement->getPoints() . ",
                                            " . $archievement->getCreator() . "," . $archievement->getCategory() . "," . $archievement->isDone() . "," . $archievement->getDateAdd() . ",
                                            " . $archievement->getDateFinish() . "," . $archievement->getDateLimit() . "," . $archievement->getIdVisual() . ")";
        $firebug = FirePHP::getInstance(true);
        $firebug->log($sql);
        $result = mysqli_query($this->db, $sql);
        $id = mysqli_insert_id($this->db);
        if ($result) {
            $sql = "INSERT INTO user_achievement(id_user, id_archievement) VALUES ('" . $archievement->getCreator() . "','" . $id . "')";
            $result = mysqli_query($this->db, $sql);
            return $archievement;
        }
        return $result;
    }
    public function finishArchievement($idArchivement){
         $sql = "UPDATE achievement SET done=1 WHERE id='".$idArchivement."'";
        $firebug = FirePHP::getInstance(true);
        $firebug->log($sql);
        $result = mysqli_query($this->db, $sql);
        $id = mysqli_insert_id($this->db);
        return $result;
        
    }

}

?>